package com.jiandaoshou;

import android.content.Intent;
import android.os.Handler;
import android.os.Message;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.OrientationHelper;
import android.support.v7.widget.RecyclerView;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;

import com.andview.refreshview.XRefreshView;
import com.andview.refreshview.XRefreshViewFooter;
import com.jiandaoshou.adapter.YinHangKaAdapter;
import com.jiandaoshou.base.BaseActivity;
import com.jiandaoshou.entry.CardInfo;
import com.jiandaoshou.http.APIDefine;
import com.jiandaoshou.http.Http;
import com.jiandaoshou.utils.DividerItemDecoration;
import com.jiandaoshou.utils.JsonUtil;

import org.json.JSONArray;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

public class YinHangKaAct extends BaseActivity {
    private RecyclerView mRecyclerView;
    private XRefreshView xRefreshView;
    private String shopId;
    private LinearLayoutManager LayoutManager;
    private YinHangKaAdapter adapter;
    private List<CardInfo> datas = new ArrayList<CardInfo>();
    private Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {
                case 0:
                    xRefreshView.setLoadComplete(true);
                    xRefreshView.stopLoadMore();
                    break;
            }
        }
    };

    @Override
    public boolean showBackLogo() {
        return true;
    }

    @Override
    protected int getContentViewRes() {
        return R.layout.activity_yinhangka;
    }

    @Override
    protected String getTitleText() {
        return "我的银行卡";
    }

    @Override
    public void init() {
        mRecyclerView = $(R.id.recycler_yinhangka);
        xRefreshView = $(R.id.refresh);
        //设置布局管理器
        LayoutManager = new LinearLayoutManager(this);
        LayoutManager.setOrientation(OrientationHelper.VERTICAL);
        mRecyclerView.setLayoutManager(LayoutManager);
        mRecyclerView.addItemDecoration(new DividerItemDecoration(this, DividerItemDecoration.VERTICAL_LIST));
        //设置增加或删除条目的动画
        mRecyclerView.setItemAnimator(new DefaultItemAnimator());
        adapter = new YinHangKaAdapter(YinHangKaAct.this, datas);
        mRecyclerView.setAdapter(adapter);
        //设置xRefreshView
        xRefreshView.setHideFooterWhenComplete(false);
        xRefreshView.setMoveForHorizontal(true);
        xRefreshView.setAutoLoadMore(true);
        xRefreshView.setPullLoadEnable(true);
        adapter.setCustomLoadMoreView(new XRefreshViewFooter(this));
        mRecyclerView.setHasFixedSize(true);
        //加载数据
        GetData(0);
        xRefreshView.setXRefreshViewListener(new XRefreshView.SimpleXRefreshListener() {
            @Override
            public void onRefresh() {
                super.onRefresh();
                datas.clear();
                GetData(1);
            }

            @Override
            public void onLoadMore(boolean isSilence) {
                super.onLoadMore(isSilence);
                handler.sendEmptyMessageDelayed(0,2000);
            }
        });
        adapter.setOnItemListener(new YinHangKaAdapter.OnItemListener() {
            @Override
            public void OnListener(View view, int pos) {

            }
        });
    }

    private void GetData(final int type) {
        HashMap<String, String> map = new HashMap<>();
        shopId = getIntent().getStringExtra("shopId");
        map.put("strShopID", shopId);
        Http.post(APIDefine.TF_Interface_GetUserCardInfo, map, new Http.HttpCallBack() {
            @Override
            public void onHttpSuccess(String response) throws Exception {
                JSONObject object = new JSONObject(response);
                JSONArray array = object.getJSONArray("CardInfoList");
                CardInfo info = null;
                for (int i = 0; i < array.length(); i++) {
                    info = JsonUtil.parser.fromJson(array.get(i).toString(), CardInfo.class);
                    datas.add(info);
                }
                adapter.notifyDataSetChanged();
                if(type==1){
                   xRefreshView.stopRefresh();
                }else{
                    xRefreshView.stopLoadMore();
                }
            }
        });
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case R.id.add:
                Intent intent=new Intent(YinHangKaAct.this,YinHangKaDetailAct.class);
                intent.putExtra("shopId",shopId);
                startActivityForResult(intent,222);
                break;
        }
        return super.onOptionsItemSelected(item);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode==222) {
            if (resultCode==RESULT_OK) {
                datas.clear();
                GetData(0);
            }
        }
    }
}
